/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Util;

import DAO.RaceDAO;
import DAO.RaceResult;
import DAO.StarterDAO;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import javafx.stage.FileChooser;
import racesuitefx.entities.Race;
import racesuitefx.entities.Starter;

/**
 *
 * @author Wolfgang
 */
public class ReportUtil {
    public static void printReport(Race r){
         FileChooser chooser = new FileChooser();
         String[] downloadableExtensions = { ".pdf"};
         chooser.setTitle(r.getName()+" Speichern");
         chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF",downloadableExtensions));
         File saveFile = chooser.showSaveDialog(null);
          try {
                Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(saveFile));
                document.open();
                addMetaData(document);
                addTitlePage(document);
                addEmptyLine(document, 1);
                addResultTable(document, r.getRaceId());
                document.newPage();
                addDetailResult(document, r.getRaceId());
                document.close();
            } catch (FileNotFoundException ex) {
               System.err.println(ex.getMessage());
            } catch (DocumentException ex) {
               System.err.println(ex.getMessage());
            }
    }
    private static void addMetaData(Document document) {
        document.addTitle("Ergebnisse");
        document.addSubject("Ergebnisse vom Rennen");
        document.addAuthor("Enduro Stammtisch Perg");
    }

    private static void addTitlePage(Document document) throws DocumentException {
        Paragraph perface = new Paragraph();
        perface.add(new Paragraph("Ergebnisse:"));
        document.add(perface);
    }

    private static void addResultTable(Document document, int raceId) throws DocumentException {
        // Create Table based on Result
         RaceDAO dao = new RaceDAO();
        List<RaceResult> results = dao.getResults(raceId);
        PdfPTable table = new PdfPTable(5);
        PdfPCell c = new PdfPCell(new Phrase("Vorname"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c);
        c = new PdfPCell(new Phrase("Nachname"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c);
        c = new PdfPCell(new Phrase("Startnummer"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c);
        c = new PdfPCell(new Phrase("Runden"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c);
        c = new PdfPCell(new Phrase("Zeit"));
        c.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c);
        table.setHeaderRows(1);
        for (RaceResult r : results) {
            table.addCell(r.getFirstName());
            table.addCell(r.getLastName());
            table.addCell("" + r.getStartNumber());
            table.addCell("" + r.getLoopcount());
            table.addCell(r.getTimeInMinutes());
        }
        document.add(table);
    }

    private static void addDetailResult(Document document, int raceId) throws DocumentException {
         RaceDAO dao = new RaceDAO();
         Race r = dao.find(raceId);
        StarterDAO starterdao = new StarterDAO();
        for (Starter s : r.getStarterCollection()) {
            // walk through the starter and create a detail report
            Paragraph perface = new Paragraph();
            perface.add(new Paragraph("Teilnehmer: " + s.getFirstName() + " " + s.getLastName() + " " + s.getStartNumber()));
            document.add(perface);
            addEmptyLine(document, 1);
            List<Date> rlist = starterdao.getDetailResult(s.getRace().getRaceId(), s.getTag().getTagId());
            PdfPTable table = new PdfPTable(2);
            PdfPCell c = new PdfPCell(new Phrase("Runde"));
            c.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c);
            c = new PdfPCell(new Phrase("Zeit"));
            c.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c);
            table.setHeaderRows(1);
            for (int i = 0; i <= rlist.size() - 2; i++) {
                Date d1 = rlist.get(i);
                Date d2 = rlist.get(i + 1);
                Date d3 = new Date();
                d3.setTime(d2.getTime() - d1.getTime());
                table.addCell("Runde: " + (i + 1));
                table.addCell("" + (d3.getHours() - 1) + ":" + d3.getMinutes() + ":" + d3.getSeconds());
            }
            document.add(table);
            addEmptyLine(document, 1);
        }
    }

    private static void addEmptyLine(Document doc, int number) throws DocumentException {
        for (int i = 0; i < number; i++) {
            doc.add(new Paragraph(" "));
        }
    }
}
